from ui.bookmanager import *
from service import *
from addBook import *
from PyQt5.QtWidgets import QHeaderView
from PyQt5.QtGui import  QStandardItemModel,QStandardItem

class BookManagerMainWindow(Ui_BookManagerMainWindow, QtWidgets.QMainWindow):
    def __init__(self, ) -> None:
        super().__init__()
        self.setupUi(self)
        self.queryBook()
        self.pushButton.clicked.connect(self.queryBook)
        self.pushButton_2.clicked.connect(self.addBook)



    def addBook(self):
        self.newWindow=AddBookMainWindow()
        self.newWindow.show()


    def queryBook(self):
        title = self.lineEdit.text()
        bs = BookService()
        if title:
            book_list = bs.getBookList(title)
        else:
            book_list = bs.getBookList()

        self.model=QStandardItemModel(4,4)#存储任意结构数据
        self.model.setHorizontalHeaderLabels(['书名','isbn','作者','可借数量'])
        row = 0
        for u in book_list:
            column = 0
            self.model.setItem(row,column,QStandardItem("%s"%(u.title)))
            self.model.setItem(row,column+1,QStandardItem("%s"%(u.isbn)))
            self.model.setItem(row,column+2,QStandardItem("%s"%(u.author)))
            self.model.setItem(row,column+3,QStandardItem("%s"%(u.count)))
            row += 1
        self.tableView.setModel(self.model)
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)